﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using IssueVision.Common;
using SL.IdentityModel.Services;

namespace IssueVision.Model
{
    [Export(typeof(IAuthenticationModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class AuthenticationModel: IAuthenticationModel
    {
        #region "Private Data"
        ClaimsIdentitySessionManager _sessionManager;
        #endregion "Private Data"

        #region "Protected Propertes"
        protected ClaimsIdentitySessionManager SessionManager
        {
            get
            {
                if (_sessionManager == null)
                {
                    _sessionManager = ClaimsIdentitySessionManager.Current;

                    _sessionManager.SignInComplete += _sessionManager_SignInComplete;
                    _sessionManager.SignOutComplete += _sessionManager_SignOutComplete;
                }
                return _sessionManager;
            }
        }
        #endregion "Protected Propertes"

        #region "IAuthenticationModel Interface implementation"
        /// <summary>
        /// Authenticate a user with user name and password
        /// </summary>
        /// <param name="userName">user name</param>
        /// <param name="password">password</param>
        public void SignInAsync(string userName, string password)
        {
            SessionManager.SignInUsernameAsync(userName, password);
            IsBusy = true;
        }

        /// <summary>
        /// Logout
        /// </summary>
        public void SignOutAsync()
        {
            SessionManager.SignOutAsync();
            IsBusy = true;
        }

        /// <summary>
        /// True if at least one call is
        /// in progress; otherwise, false
        /// </summary>
        public Boolean IsBusy
        {
            get
            {
                return _isBusy;
            }

            private set
            {
                if (_isBusy != value)
                {
                    _isBusy = value;
                    OnPropertyChanged("IsBusy");
                }
            }
        }
        private Boolean _isBusy;

        public event EventHandler<SignInEventArgs> SignInCompleted;
        public event EventHandler<SignOutEventArgs> SignOutCompleted;
        #endregion "IAuthenticationModel Interface implementation"

        #region "INotifyPropertyChanged Interface implementation"
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion "INotifyPropertyChanged Interface implementation"

        #region "Private Methods"

        /// <summary>
        /// Event handler for SignInComplete
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _sessionManager_SignInComplete(object sender, SignInEventArgs e)
        {
            IsBusy = false;
            if (SignInCompleted != null)
                SignInCompleted(this, e);
        }

        /// <summary>
        /// Event handler for SignOutComplete
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _sessionManager_SignOutComplete(object sender, SignOutEventArgs e)
        {
            IsBusy = false;
            if (SignOutCompleted != null)
                SignOutCompleted(this, e);
        }

        #endregion "Private Methods"
    }
}
